home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW re2c 1.1 / examples / cnokw.re < prev    next >
Encoding:
Text File  |  1995-06-01  |  4.5 KB  |  240 lines  |  [TEXT/MPS ]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define    ADDEQ    257
  6. #define    ANDAND    258
  7. #define    ANDEQ    259
  8. #define    ARRAY    260
  9. #define    ASM    261
  10. #define    AUTO    262
  11. #define    BREAK    263
  12. #define    CASE    264
  13. #define    CHAR    265
  14. #define    CONST    266
  15. #define    CONTINUE    267
  16. #define    DECR    268
  17. #define    DEFAULT    269
  18. #define    DEREF    270
  19. #define    DIVEQ    271
  20. #define    DO    272
  21. #define    DOUBLE    273
  22. #define    ELLIPSIS    274
  23. #define    ELSE    275
  24. #define    ENUM    276
  25. #define    EQL    277
  26. #define    EXTERN    278
  27. #define    FCON    279
  28. #define    FLOAT    280
  29. #define    FOR    281
  30. #define    FUNCTION    282
  31. #define    GEQ    283
  32. #define    GOTO    284
  33. #define    ICON    285
  34. #define    ID    286
  35. #define    IF    287
  36. #define    INCR    288
  37. #define    INT    289
  38. #define    LEQ    290
  39. #define    LONG    291
  40. #define    LSHIFT    292
  41. #define    LSHIFTEQ    293
  42. #define    MODEQ    294
  43. #define    MULEQ    295
  44. #define    NEQ    296
  45. #define    OREQ    297
  46. #define    OROR    298
  47. #define    POINTER    299
  48. #define    REGISTER    300
  49. #define    RETURN    301
  50. #define    RSHIFT    302
  51. #define    RSHIFTEQ    303
  52. #define    SCON    304
  53. #define    SHORT    305
  54. #define    SIGNED    306
  55. #define    SIZEOF    307
  56. #define    STATIC    308
  57. #define    STRUCT    309
  58. #define    SUBEQ    310
  59. #define    SWITCH    311
  60. #define    TYPEDEF    312
  61. #define    UNION    313
  62. #define    UNSIGNED    314
  63. #define    VOID    315
  64. #define    VOLATILE    316
  65. #define    WHILE    317
  66. #define    XOREQ    318
  67. #define    EOI    319
  68.  
  69. typedef unsigned int uint;
  70. typedef unsigned char uchar;
  71.  
  72. #define    BSIZE    8192
  73.  
  74. #define    YYCTYPE        uchar
  75. #define    YYCURSOR    cursor
  76. #define    YYLIMIT        s->lim
  77. #define    YYMARKER    s->ptr
  78. #define    YYFILL(n)    {cursor = fill(s, cursor);}
  79.  
  80. #define    RET(i)    {s->cur = cursor; return i;}
  81.  
  82. typedef struct Scanner {
  83.     int            fd;
  84.     uchar        *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof;
  85.     uint        line;
  86. } Scanner;
  87.  
  88. uchar *fill(Scanner *s, uchar *cursor){
  89.     if(!s->eof){
  90.     uint cnt = s->tok - s->bot;
  91.     if(cnt){
  92.         memcpy(s->bot, s->tok, s->lim - s->tok);
  93.         s->tok = s->bot;
  94.         s->ptr -= cnt;
  95.         cursor -= cnt;
  96.         s->pos -= cnt;
  97.         s->lim -= cnt;
  98.     }
  99.     if((s->top - s->lim) < BSIZE){
  100.         uchar *buf = (uchar*) malloc(((s->lim - s->bot) + BSIZE)*sizeof(uchar));
  101.         memcpy(buf, s->tok, s->lim - s->tok);
  102.         s->tok = buf;
  103.         s->ptr = &buf[s->ptr - s->bot];
  104.         cursor = &buf[cursor - s->bot];
  105.         s->pos = &buf[s->pos - s->bot];
  106.         s->lim = &buf[s->lim - s->bot];
  107.         s->top = &s->lim[BSIZE];
  108.         free(s->bot);
  109.         s->bot = buf;
  110.     }
  111.     if((cnt = read(s->fd, (char*) s->lim, BSIZE)) != BSIZE){
  112.         s->eof = &s->lim[cnt]; *(s->eof)++ = '\n';
  113.     }
  114.     s->lim += cnt;
  115.     }
  116.     return cursor;
  117. }
  118.  
  119. int scan(Scanner *s){
  120.     uchar *cursor = s->cur;
  121. std:
  122.     s->tok = cursor;
  123. /*!re2c
  124. any    = [\000-\377];
  125. O    = [0-7];
  126. D    = [0-9];
  127. L    = [a-zA-Z_];
  128. H    = [a-fA-F0-9];
  129. E    = [Ee] [+-]? D+;
  130. FS    = [fFlL];
  131. IS    = [uUlL]*;
  132. ESC    = [\\] ([abfnrtv?'"\\] | "x" H+ | O+);
  133. */
  134.  
  135. /*!re2c
  136.     "/*"            { goto comment; }
  137.     
  138.     L (L|D)*        { RET(ID); }
  139.     
  140.     ("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) |
  141.     (['] (ESC|any\[\n\\'])* ['])
  142.                 { RET(ICON); }
  143.     
  144.     (D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?)
  145.                 { RET(FCON); }
  146.     
  147.     (["] (ESC|any\[\n\\"])* ["])
  148.                 { RET(SCON); }
  149.     
  150.     "..."                   { RET(ELLIPSIS); }
  151.     ">>="            { RET(RSHIFTEQ); }
  152.     "<<="            { RET(LSHIFTEQ); }
  153.     "+="            { RET(ADDEQ); }
  154.     "-="            { RET(SUBEQ); }
  155.     "*="            { RET(MULEQ); }
  156.     "/="            { RET(DIVEQ); }
  157.     "%="            { RET(MODEQ); }
  158.     "&="            { RET(ANDEQ); }
  159.     "^="            { RET(XOREQ); }
  160.     "|="            { RET(OREQ); }
  161.     ">>"            { RET(RSHIFT); }
  162.     "<<"            { RET(LSHIFT); }
  163.     "++"            { RET(INCR); }
  164.     "--"            { RET(DECR); }
  165.     "->"            { RET(DEREF); }
  166.     "&&"            { RET(ANDAND); }
  167.     "||"            { RET(OROR); }
  168.     "<="            { RET(LEQ); }
  169.     ">="            { RET(GEQ); }
  170.     "=="            { RET(EQL); }
  171.     "!="            { RET(NEQ); }
  172.     ";"            { RET(';'); }
  173.     "{"            { RET('{'); }
  174.     "}"            { RET('}'); }
  175.     ","            { RET(','); }
  176.     ":"            { RET(':'); }
  177.     "="            { RET('='); }
  178.     "("            { RET('('); }
  179.     ")"            { RET(')'); }
  180.     "["            { RET('['); }
  181.     "]"            { RET(']'); }
  182.     "."            { RET('.'); }
  183.     "&"            { RET('&'); }
  184.     "!"            { RET('!'); }
  185.     "~"            { RET('~'); }
  186.     "-"            { RET('-'); }
  187.     "+"            { RET('+'); }
  188.     "*"            { RET('*'); }
  189.     "/"            { RET('/'); }
  190.     "%"            { RET('%'); }
  191.     "<"            { RET('<'); }
  192.     ">"            { RET('>'); }
  193.     "^"            { RET('^'); }
  194.     "|"            { RET('|'); }
  195.     "?"            { RET('?'); }
  196.  
  197.  
  198.     [ \t\v\f]+        { goto std; }
  199.  
  200.     "\n"
  201.         {
  202.         if(cursor == s->eof) RET(EOI);
  203.         s->pos = cursor; s->line++;
  204.         goto std;
  205.         }
  206.  
  207.     any
  208.         {
  209.         printf("unexpected character: %c\n", *s->tok);
  210.         goto std;
  211.         }
  212. */
  213.  
  214. comment:
  215. /*!re2c
  216.     "*/"            { goto std; }
  217.     "\n"
  218.         {
  219.         if(cursor == s->eof) RET(EOI);
  220.         s->tok = s->pos = cursor; s->line++;
  221.         goto comment;
  222.         }
  223.         any            { goto comment; }
  224. */
  225. }
  226.  
  227. main(){
  228.     Scanner in;
  229.     int t;
  230.     memset((char*) &in, 0, sizeof(in));
  231.     in.fd = 0;
  232.     while((t = scan(&in)) != EOI){
  233. /*
  234.     printf("%d\t%.*s\n", t, in.cur - in.tok, in.tok);
  235.     printf("%d\n", t);
  236. */
  237.     }
  238.     close(in.fd);
  239. }
  240.